//geo [[名字，lon,lat],[],[]]
function FaultChartMan(){
	var height = 0;
	var width = 0;
	var bigHeight = 0;
	var bigWidth = 0;
	initSize();
	function initSize(){
		var extent = map.getExtent();
		var rightbottom = new SuperMap.LonLat(extent.right,extent.bottom);
		var rightbottompx = map.getPixelFromLonLat(rightbottom);
		height = rightbottompx.y/6;
		width = rightbottompx.x/7;
		bigHeight =  rightbottompx.y*0.5;
		bigWidth = rightbottompx.x*0.5;
	}
	var years = [];
	var data = [];
	var etypes = [ "母线", "线路", "变压器", "发电机" ];
	var ranks = [ "超高压公司", "调峰调频公司", "广东电网", "广州电网", "深圳电网", "广西电网", "云南电网",
			"贵州电网", "海南电网", "总调直调电厂 ", "广东中调电厂", "广州中调电厂", "深圳中调电厂", "广西中调电厂",
			"云南中调电厂", "贵州中调电厂", "海南中调电厂" ];
	var charts = new Map();
	var geoArr = [/*['超高压公司',113.3186262846,23.1334242590],*/
	   //           ['调峰调频公司',113.3415055275,23.1374841211],
	   //           ['总调直调电厂',106.5470148,19.71165311],
	              ['海南电网',110.3529667854,20.0312124821],
	    //          ['海南中调电厂',110.1998900000,20.0442200000],//市政府
	  ['广东电网',113.3083100000,26.1315700000],
	 // ['广东中调电厂',113.2662700000,23.1317100000],//广东省政府
	  ['广西电网',108.3371204138,22.8266621833],
	//  ['广西中调电厂',108.3662700000,22.8172400000],//南宁市政府
	  ['贵州电网',106.7179727554,26.5718896028],
	//  ['贵州中调电厂',106.7065500000,26.5990700000],//贵州省政府
	  ['云南电网',102.7307030000,25.0394470000],
	//  ['云南中调电厂',102.7167700000,25.0525650000],//云南省政府
	  ['广州电网',113.3294400000,23.1310300000],
	//  ['广州中调电厂',113.2643600000,23.1290800000],//广州市政府
	  ['深圳电网',114.0505474060,22.5337536239],
	/*['深圳中调电厂',114.1505474060,22.4037536239]*/];//深圳市政府
	var geo0 = [//['超高压公司',113.3186262846,23.3334242590,115.07426,32.56673],
	       	    //          ['调峰调频公司',113.3415055275,23.1374841211,109.83964,32.36673],
	       	     //         ['总调直调电厂',106.5470148,20.01165311,121.68803,32.48673],
	       	              ['海南电网',110.3529667854,20.0312124821,104.87555,20.48740],
	       	     //         ['海南中调电厂',110.1998900000,19.5442200000,105.07843,17.26958],//市政府
	       	  ['广东电网',113.3294400000,23.0010300000,113.75026,29.38632],
	       	//  ['广东中调电厂',113.9662700000,23.0017100000,120.51398,29.19624],//广东省政府
	       	  ['广西电网',108.3371204138,22.8266621833,105.67170,24.23042],
	       //	  ['广西中调电厂',107.8662700000,22.8172400000,99.36955,23.37348],//南宁市政府
	       	  ['贵州电网',106.7179727554,26.5718896028,104.30041,31.12983],
	       //	  ['贵州中调电厂',106.7065500000,26.0990700000,105.43183,27.91060],//贵州省政府
	       	  ['云南电网',102.7307030000,25.0394470000,98.25080,30.42000],
	       //	  ['云南中调电厂',102.7167700000,24.5025650000,98.23371,27.24340],//云南省政府
	       	  ['广州电网',113.3083100000,22.800,113.73004,25.18771],
	       //	  ['广州中调电厂',113.8043600000,23.1290800000,120.76691,25.85157],//广州市政府
	       	  ['深圳电网',114.0505474060,22.5337536239,112.60505,21.27769],
	      /* 	['深圳中调电厂',114.1505474060,22.0037536239,112.59308,18.00270]*/];//深圳市政府
	var infoWins = new Map();
	this.show = function(){
		if(data.length == 0){
			getData();
		}else{
			draw();
		}
	};
	this.isShow = function(){
		var pops = infoWins.values();
		var all = map.popups;
		return pops.some(function(value, index, array) {
			return all.indexOf(value) != -1;
		});
	};
	function draw(){
		infoWins.clear();
		map.removeAllPopup();
		for(var i=0;i<geo0.length;i++){
			var geo = geo0[i];
			var zoom = map.getZoom();
			var lon = geo[1];
			var lat = geo[2];
			if(zoom<10){
				lon = (geo[1]-geo[3])/4*(zoom-6)+geo[3];
				lat = (geo[2]-geo[4])/4*(zoom-6)+geo[4];
			}
			if(geo[0]=='广州'){
				console.log(lon,lat);
			}
			var id = addDiv(geo[0],lon,lat,height,width);
			var chart = initChart(id,geo[0],data[geo[0]]);
		}
	}
	var rollNum = 0;
	function getData(){
		//[{rank:[type[{year,count}}]
		$.getJSON("/rest/fault/getFaultDatasByGroupRankTypeYear",function(result){
			data = result;
			initYears(data);
			draw();
		});
	}
	function initYears(data){
		years = [];
		for(var rank in data){
			var ra = data[rank];
			for(var etype in ra){
				var et = ra[etype];
				for(var year in et){
					year = Number(year);
					if(years.indexOf(year) == -1){
						years.push(year);
					}
				}
			}
		}
		years.sort(sortNumber);
		function sortNumber(a, b){
			return a - b;
		}
	}
	function initChart(divid,name,data,big){
		var bar = new CategoryBar(divid);
		bar.setTitle(name);
		bar.setTitlePosition("center");
		bar.setToolbox(false);
		bar.setxAxis(years);
		for(var i in etypes){
			var et = [];
			if(data && data[etypes[i]]){
				et = data[etypes[i]];
			}
			var yAxisdata = [];
			for(var j in years){
				var count = 0;
				if(et && et[years[j]]){
					count = et[years[j]];
				}
 				yAxisdata[j] = count;
			}
			bar.setyAxis(yAxisdata);
			bar.setyAxisName(etypes[i]);
			if(!big){
				bar.setyAxisLabelShow(false);
			}
		}
		var option = bar.getOption();
		option.grid = {
				top:30,
				bottom:0,
				left:0,
				right:0,
				containLabel:true
		};
		if(big){
			option.grid.top = 50;
			bar.setTitlePosition("left");
			bar.setToolbox(true);
			option.legend.show = true;
			bar.show();
		}else{
			option.graphic = [{
		        id: 'zoom-btn',
		        type: 'rect',
		        style: {
		            fill: '#F5FFFA',
		            stroke:'black'
		        },
		        shape:{
		        	width:10,
		        	height:8
		        },
		        right:10,
		        top: 10,
		        onclick: function () {
		        	var id = addBigDiv(name);
					initChart(id,name,data,true);
		        }
		    },];
			option.title.textStyle.fontSize = 14;
			option.legend.show = false;
			option.tooltip.confine = true;
			option.tooltip.textStyle.fontSize = 10;
			bar.show();
			charts.put(name,bar.getChart());
		}
		return bar.getChart();
	}
	function addBigDiv(name){
		var g = map.getExtent();
		var id = addDiv(name,g.right,g.bottom,bigHeight,bigWidth,true);
		return id;
	}
	function addDiv(rank,lon,lat,height,width,closeonmove){
		//画出一个div来，取一个id以时间来
		var time = new Date();
		var id = time.getMilliseconds()+rank;
		var div = $('<div id="'+id+'"></div>');
		div.css('height',height+"px");
		div.css('width',width+"px");
		div.css('overflow-x','hidden');
		div.css('overflow-y','hidden');
		var pdiv = $("<div />");
		pdiv.css('height',height+"px");
		pdiv.css('width',width+"px");
		pdiv.append(div);
		var content = pdiv.html();
		if(closeonmove){
			var popup = new SuperMap.Popup.Anchored(null,
					new SuperMap.LonLat(lon,lat),
					null,
					content,
					null,
					true,
					null);
			popup.keepInMap = true;
			popup.closeOnMove = true;
			popup.setBackgroundColor('white');
			popup.setOpacity(1);
		}else{
			var popup = new SuperMap.Popup(null,
					new SuperMap.LonLat(lon,lat),
					null,
					content,
					false,
					null);
			popup.keepInMap = false;
			popup.closeOnMove = false;
			popup.setBackgroundColor('#F5FFFA');
			popup.setOpacity(0.9);
			infoWins.put(rank,popup);
		}
		popup.autoSize = true;
		map.addPopup(popup);
		return id;
	};
}
